﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DblogStudio.BLL;
using DblogStudio.DAL.Models;
using DblogStudio.GUI.Common;
using DblogStudio.GUI.Utils;
using DblogStudio.Utils;

namespace DblogStudio.GUI.DuLieu
{
    public partial class FormQuanLyChiTietMay_ChiTiet : BaseDialog
    {
        BoPhanMay boPhanMay;
        ChiTietMay chiTietMay;
        public event Common.TListChangedEventHandler OnChanged;

        public FormQuanLyChiTietMay_ChiTiet(BoPhanMay boPhanMay)
        {
            //Thêm mới
            InitializeComponent();
            this.boPhanMay = boPhanMay;
            LoadBoPhanMay();
            InitInterface();
            if (boPhanMay != null)
                cmbBoPhanMay.SelectedValue = boPhanMay.MaBoPhanMay;
        }
        public FormQuanLyChiTietMay_ChiTiet(ChiTietMay chiTietMay)
        {
            //Chỉnh sửa
            InitializeComponent();
            this.chiTietMay = chiTietMay;
            LoadBoPhanMay();
            InitInterface();
        }

        void InitInterface()
        {
            if (chiTietMay != null)
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.EDIT, ENTITY_NAME.CHITIETMAY);
                chkTiepTucBoSung.Checked = false;
                chkTiepTucBoSung.Visible = false;
                txtTenChiTietMay.Text = chiTietMay.TenChiTietMay;
                txtGhiChu.Text = chiTietMay.GhiChu;
                cmbBoPhanMay.SelectedValue = chiTietMay.MaBoPhanMay;
            }
            else
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.ADDNEW, ENTITY_NAME.CHITIETMAY);
                chkTiepTucBoSung.Visible = true;
                chkTiepTucBoSung.Checked = true;
                txtTenChiTietMay.Text = "";
                txtGhiChu.Text = "";
                ;
            }
        }

        void LoadBoPhanMay()
        {
            if (boPhanMay != null)
            {
                cmbBoPhanMay.DataSource = BizBoPhanMay.GetList(boPhanMay.MaMaySanXuat);
                cmbBoPhanMay.DisplayMember = "TenBoPhanMay";
                cmbBoPhanMay.ValueMember = "MaBoPhanMay";
            }
            else if (chiTietMay != null)
            {
                var boPhanMay2 = BizBoPhanMay.GetByID(chiTietMay.MaBoPhanMay);
                if (boPhanMay2 != null)
                {
                    cmbBoPhanMay.DataSource = BizBoPhanMay.GetList(boPhanMay2.MaMaySanXuat);
                    cmbBoPhanMay.DisplayMember = "TenBoPhanMay";
                    cmbBoPhanMay.ValueMember = "MaBoPhanMay";
                }
            }
        }

        bool ValidateData()
        {
            errorNotify.Clear();
            if (cmbBoPhanMay.SelectedValue == null)
            {
                errorNotify.SetError(cmbBoPhanMay, "Không được bỏ trống Bộ phận máy");
                cmbBoPhanMay.Focus();
                return false;
            }
            if (txtTenChiTietMay.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtTenChiTietMay, string.Format(ERROR_CONSTANT_STRING.LOI_FIELD_EMPTY, ENTITY_NAME.CHITIETMAY));
                txtTenChiTietMay.Focus();
                return false;
            }
            return true;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                if (chiTietMay != null)
                {
                    #region Cập nhật thông tin
                    chiTietMay.TenChiTietMay = txtTenChiTietMay.Text;
                    chiTietMay.GhiChu = txtGhiChu.Text;
                    chiTietMay.MaBoPhanMay = cmbBoPhanMay.SelectedValue.ToString();
                    var rs = BizChiTietMay.Update(chiTietMay);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        OnChanged(this, null);
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTenChiTietMay, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.CHITIETMAY));
                        txtTenChiTietMay.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_CAPNHAT, ENTITY_NAME.CHITIETMAY));
                        return;
                    }

                    #endregion
                }
                else
                {
                    #region Thêm mới
                    chiTietMay = new ChiTietMay();
                    chiTietMay.TenChiTietMay = txtTenChiTietMay.Text;
                    chiTietMay.GhiChu = txtGhiChu.Text;
                    chiTietMay.MaBoPhanMay = cmbBoPhanMay.SelectedValue.ToString();
                    var rs = BizChiTietMay.Add(chiTietMay);
                    chiTietMay = null;
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        //Sau khi thêm thành công kiểm tra có cho nhập thêm hay không
                        OnChanged(this, null);
                        if (chkTiepTucBoSung.Checked)
                        {
                            InitInterface();
                            txtTenChiTietMay.Focus();
                        }
                        else
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTenChiTietMay, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.CHITIETMAY));
                        txtTenChiTietMay.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_THEM, ENTITY_NAME.CHITIETMAY));
                        return;
                    }
                    #endregion
                }
            }
        }

        private void FormQuanLyGiangVien_ChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

        private void cmbDonVi_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
    }
}
